package cn.yunhe.servlet;


import cn.yunhe.entity.LeaveMessage;
import cn.yunhe.service.LeaveMessageService;
import cn.yunhe.util.PageUtil;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/GetLeaveMessageServlet")
public class GetLeaveMessageServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        LeaveMessageService leaveMessageService=new LeaveMessageService();
        String currPage=request.getParameter("pageIndex");
        if (currPage==null) {
            currPage = "1";
        }
            int pageIndex = Integer.parseInt(currPage);

            int totalDatas=leaveMessageService.getleaveMessageDaoCount();
            int totalPages=PageUtil.getTotalPages(totalDatas);
            if (pageIndex < 1) {
                pageIndex = 1;
            }
            if (pageIndex > totalPages) {
                pageIndex = totalPages;
            }
            //得到关键字

            List<LeaveMessage> leaveMessageList=leaveMessageService.getLeaveMessage(pageIndex);
            request.setAttribute("pageIndex", pageIndex);
            request.setAttribute("leaveMessageList",leaveMessageList);
            request.setAttribute("totalPages", totalPages);
           request.getRequestDispatcher("manage/guestbook.jsp").forward(request, response);
        }


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
